package cn.cqzaier.pub.api.plugin.cuteCat;

import cn.cqzaier.pub.api.Result;
import cn.cqzaier.pub.api.plugin.WxrebootCommonApiInterface;
import cn.cqzaier.pub.api.plugin.base.reqeust.cutecat.*;
import cn.cqzaier.pub.api.plugin.cuteCat.response.GroupInfo;
import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.BeanUtils;

import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.function.Function;

/**
 * 可爱猫Api
 */
public class WxrebootCuteCatApiImpl implements WxrebootCommonApiInterface {

    private CuteCatRebootRequest cuteCatRebootRequest;
    private CuteCatRequestUtil cuteCatRequestUtil;


    @Override
    public String getRequestUrl() {
        return this.cuteCatRequestUtil.getRequstUrl();
    }


    public WxrebootCuteCatApiImpl(CuteCatRebootRequest cuteCatRebootRequest, CuteCatRequestUtil CuteCatRequestUtil) {
        this.cuteCatRebootRequest = cuteCatRebootRequest;
        this.cuteCatRequestUtil = CuteCatRequestUtil;
    }

    @Override
    public Map<String, Object> getFriendlist(String rebootId) {
        this.cuteCatRebootRequest.setEvent("GetFriendList");
        return ((CuteCatRequestUtil<CuteCatRebootRequest, Map<String, Object>>) cuteCatRequestUtil).doReq(this.cuteCatRebootRequest, new Function<String, Map<String, Object>>() {
            @Override
            public Map<String, Object> apply(String s) {
                System.out.println();
                return null;
            }
        });
    }

    @Override
    public List<GroupInfo> getGrouplist(String rebootId) {
        this.cuteCatRebootRequest.setEvent("GetGrouplist");
        return ((CuteCatRequestUtil<CuteCatRebootRequest, List<GroupInfo>>) cuteCatRequestUtil).doReq(this.cuteCatRebootRequest, new Function<String, List<GroupInfo>>() {
            @Override
            public List<GroupInfo> apply(String s) {
                if (cuteCatRequestUtil.isHealth()) {
                    return new Result<GroupInfo>().getResultArryByKeySplitDone(s, GroupInfo.class, "ReturnJson");
                }
                return null;
            }
        });
    }

    @Override
    public JSONObject sendGroupTxtMsg(String rebootId, String groupId, String msg) {
        CuteCatTxtRebootRequest cuteCatTxtRebootRequest = new CuteCatTxtRebootRequest();
        this.cuteCatRebootRequest.setEvent("SendTextMsg");
        this.cuteCatRebootRequest.setTo_wxid(groupId);
        copyProperty(cuteCatTxtRebootRequest);
        cuteCatTxtRebootRequest.setMsg(msg);
        return ((CuteCatRequestUtil<CuteCatTxtRebootRequest, JSONObject>) cuteCatRequestUtil).doReq(cuteCatTxtRebootRequest, new Function<String, JSONObject>() {
            @Override
            public JSONObject apply(String s) {
                if (cuteCatRequestUtil.isHealth()) {
                    return JSONObject.parseObject(s, JSONObject.class);
                }
                return null;
            }
        });
    }

    @Override
    public JSONObject sendPrivateTxtMsg(String rebootId, String groupId, String msg) {
        return sendGroupTxtMsg(rebootId, groupId, msg);
    }

    @Override
    public JSONObject sendImageMsg(String rebootId, String groupId, String pic) {
        CuteCatSendFileAndImageRequest wechatSendImage = new CuteCatSendFileAndImageRequest();
        copyProperty(wechatSendImage);
        CuteCatSendFileAndImage cuteCatSendFileAndImage = new CuteCatSendFileAndImage();

        setUrl(pic, cuteCatSendFileAndImage);
        cuteCatSendFileAndImage.setName("wxupload" + UUID.randomUUID().toString() + ".png");
        wechatSendImage.setMsg(cuteCatSendFileAndImage);
        wechatSendImage.setTo_wxid(groupId);
        wechatSendImage.setEvent("SendImageMsg");
        return sendFileOrImage(wechatSendImage);
    }

    private void setUrl(String pic, CuteCatSendFileAndImage cuteCatSendFileAndImage) {
        if (pic.startsWith("http")) {
            cuteCatSendFileAndImage.setUrl(pic);
        } else {
            cuteCatSendFileAndImage.setPath(pic);//本地文件发送
        }
    }

    private void copyProperty(Object wechatSendImage) {
        BeanUtils.copyProperties(this.cuteCatRebootRequest, wechatSendImage);
    }

    private JSONObject sendFileOrImage(CuteCatSendFileAndImageRequest wechatSendImage) {
        return ((CuteCatRequestUtil<CuteCatSendFileAndImageRequest, JSONObject>) cuteCatRequestUtil).doReq(wechatSendImage, new Function<String, JSONObject>() {
            @Override
            public JSONObject apply(String s) {
                if (cuteCatRequestUtil.isHealth()) {
                    return JSONObject.parseObject(s, JSONObject.class);
                }
                return null;
            }
        });
    }


    @Override
    public JSONObject sendFile(String rebootId, String groupId, String pic) {
        CuteCatSendFileAndImageRequest wechatSendImage = new CuteCatSendFileAndImageRequest();
        copyProperty(wechatSendImage);
        CuteCatSendFileAndImage cuteCatSendFileAndImage = new CuteCatSendFileAndImage();
        setUrl(pic, cuteCatSendFileAndImage);
        cuteCatSendFileAndImage.setUrl(pic);
        cuteCatSendFileAndImage.setName("wxupload" + UUID.randomUUID().toString() + "." + FileUtil.extName(pic));
        wechatSendImage.setTo_wxid(groupId);
        wechatSendImage.setEvent("SendFileMsg");
        return sendFileOrImage(wechatSendImage);
    }

    @Override
    public JSONObject sendShareLink(String rebootId, String groupId, String title, String desc, String imageUrl, String url) {
        CuteCatShareLinkebootRequest cuteCatShareLinkebootRequest = new CuteCatShareLinkebootRequest();
        CuteCatShareLink wechatShareLink = new CuteCatShareLink();
        copyProperty(cuteCatShareLinkebootRequest);
        cuteCatShareLinkebootRequest.setEvent("SendLinkMsg");
        cuteCatShareLinkebootRequest.setTo_wxid(groupId);
        wechatShareLink.setPic_url(imageUrl);
        wechatShareLink.setTarget_url(url);
        wechatShareLink.setText(title);
        cuteCatShareLinkebootRequest.setMsg(wechatShareLink);
        return ((CuteCatRequestUtil<CuteCatShareLinkebootRequest, JSONObject>) cuteCatRequestUtil).doReq(cuteCatShareLinkebootRequest, new Function<String, JSONObject>() {
            @Override
            public JSONObject apply(String s) {
                if (cuteCatRequestUtil.isHealth()) {
                    return JSONObject.parseObject(s, JSONObject.class);
                }
                return null;
            }
        });
    }


    @Override
    public List<JSONObject> getLoggedAccountList() {
        this.cuteCatRebootRequest.setEvent("GetLoggedAccountList");
        this.cuteCatRebootRequest.setRobot_wxid(null);
        return  ((CuteCatRequestUtil<CuteCatRebootRequest, List<JSONObject>>) cuteCatRequestUtil).doReq(this.cuteCatRebootRequest, new Function<String, List<JSONObject>>() {
            @Override
            public List<JSONObject> apply(String s) {
                if (cuteCatRequestUtil.isHealth()) {
                    return new Result<JSONObject>().getResultArryByKeySplitDone(s, JSONObject.class,"data");
                }
                return null;
            }
        });
    }

    @Override
    public JSONObject sendGroupMsgAndAt(String rebootId, String groupId, String memberId, String msg) {
//        return msgTypeSend("SendGroupMsgAndAt",rebootId, groupId, memberId, msg);
        return null;
    }

    private JSONObject msgTypeSend(String event,String rebootId, String groupId, String memberId, String msg) {
        CuteCatTxtRebootRequest cuteCatTxtRebootRequest = new CuteCatTxtRebootRequest();
        this.cuteCatRebootRequest.setEvent(event);
        this.cuteCatRebootRequest.setRobot_wxid(rebootId);
        this.cuteCatRebootRequest.setGroup_wxid(groupId);
        this.cuteCatRebootRequest.setMember_wxid(memberId);
        copyProperty(cuteCatTxtRebootRequest);
        cuteCatTxtRebootRequest.setMsg(msg);
        return ((CuteCatRequestUtil<CuteCatTxtRebootRequest, JSONObject>) cuteCatRequestUtil).doReq(cuteCatTxtRebootRequest, new Function<String, JSONObject>() {
            @Override
            public JSONObject apply(String s) {
                if (cuteCatRequestUtil.isHealth()) {
                    return JSONObject.parseObject(s, JSONObject.class);
                }
                return null;
            }
        });
    }

    @Override
    public JSONObject agreeFriendVerify(String rebootId, JSONObject jsonObject) {
        return msgTypeSend("AgreeFriendVerify",rebootId, null, null, null);
    }

    @Override
    public JSONObject removeGroupMember(String rebootId, String groupId, String memberId, JSONObject otherInfo) {
        return msgTypeSend("RemoveGroupMember",rebootId, groupId, memberId, null);
    }

    @Override
    public JSONObject sendVideo(String rebootId, String groupId, String videoUrl) {
        return null;
    }


    @Override
    public JSONObject sendGroupMsgAndAtAll(String rebootId, String groupId, String msg) {
        return msgTypeSend("SendMsgAtAll",rebootId, groupId,  null,msg);
    }

    @Override
    public JSONObject downloadFileUrlLocal(String requestUrl,String url,String path) {
        return null;
    }

    @Override
    public boolean revokeMsg(String rebootId, String msgId) {
        CuteRevokeMsgRequest cuteRevokeMsgRequest = new CuteRevokeMsgRequest();
        this.cuteCatRebootRequest.setEvent("RevokeMsg");
        copyProperty(cuteRevokeMsgRequest);
        cuteRevokeMsgRequest.setMsg(msgId);
        return ((CuteCatRequestUtil<CuteRevokeMsgRequest, Boolean>) cuteCatRequestUtil).doReq(cuteRevokeMsgRequest, new Function<String, Boolean>() {
            @Override
            public Boolean apply(String s) {
                if (cuteCatRequestUtil.isHealth()) {
                    if (JSONObject.parseObject(s, JSONObject.class).getInteger("code") == 0){
                        return true;
                    };
                }
                return false;
            }
        });
    }
}
